using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using LightCAD.MathLib;
namespace LightCAD.Core.Elements
{
    public class LcLine : LcCurve2d
    {
        public Line2d Line => (this.Curve as Line2d);
        public Vector2 Start { get=> Line.Start; set { this.Line.Start = value; } }
        public Vector2 End { get => Line.End; set { this.Line.End = value; } }

        public LcLine()
        {
            this.Type = BuiltinElementType.Line;
            this.Curve = new Line2d();
        }
        public LcLine(Vector2 start, Vector2 end) : this()
        {
            this.Start = start;
            this.End = end;
        }
        public LcLine(double startX, double startY, double endX, double endY) : this(new Vector2(startX,startY),new Vector2(endX,endY))
        {
                                
        }
        public double Length=>this.Line.Length;
        [JsonIgnore]
        public double Angle=>(this.End - this.Start).Angle();

        [JsonIgnore]
        public double DeltaX=>this.End.X - this.Start.X;
      
        [JsonIgnore]
        public double DeltaY=> this.End.Y - this.Start.Y;
   

        public override LcElement ApplyMatrix(Matrix3 matrix)
        {
            this.Start = matrix.MultiplyPoint(this.Start);
            this.End = matrix.MultiplyPoint(this.End);
            return this;
        }
       
        public override LcElement Clone()
        {
            var clone = Document.CreateObject<LcLine>();
            clone.Copy(this);
            return clone;
        }

        public void Set(Vector2 start = null, Vector2 end = null, bool fireChangedEvent = true)
        {
            //PropertySetter:Start,End
            if (!fireChangedEvent)
            {
                if (start != null) this.Start = start;
                if (end != null) this.End = end;
            }
            else
            {
                bool chg_start = (start != null && start != this.Start);
                if (chg_start)
                {
                    OnPropertyChangedBefore(nameof(Start), this.Start, start);
                    var oldValue = this.Start;
                    this.Start = start;
                    OnPropertyChangedAfter(nameof(Start), oldValue, this.Start);
                }
                bool chg_end = (end != null && end != this.End);
                if (chg_end)
                {
                    OnPropertyChangedBefore(nameof(End), this.End, end);
                    var oldValue = this.End;
                    this.End = end;
                    OnPropertyChangedAfter(nameof(End), oldValue, this.End);
                }
            }
        }
        public override Box2 GetBoundingBox()
        {
            return new Box2().SetFromPoints(this.Start, this.End);
        }
        public override Box2 GetBoundingBox(Matrix3 matrix)
        {
            return new Box2().SetFromPoints(matrix.MultiplyPoint(this.Start), matrix.MultiplyPoint(this.End));
        }
        public override void WriteProperties(Utf8JsonWriter writer, JsonSerializerOptions soptions)
        {
            writer.WriteVector2dProperty(nameof(this.Start), this.Start);
            writer.WriteVector2dProperty(nameof(this.End), this.End);
        }
        public override void ReadProperties(ref JsonElement jele)
        {
            this.Start = jele.ReadVector2dProperty(nameof(Start));
            this.End = jele.ReadVector2dProperty(nameof(End));
        }
    }
}